/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import models.Hug;
import models.Relationship;
import models.User;

/**
 *
 * @author luci
 */
@Named(value = "friendsListBean")
@SessionScoped
public class FriendsList implements Serializable {
    
    private Integer initialIndex = 0;
    private List<User> friends = new ArrayList<User>();
    private int numberOfFriends = 10;   
    private boolean error = false;
    private int userId = -1;
    private String message = null;
    private boolean displayMessage = false;
    private String search;
    private List<User> users = new ArrayList<User>();
    private List<User> hugs;
    
    
    
    
    private void getFriendsFromDB(int id){
        error = false;
        List<User> newFriends = null;
        newFriends = Relationship.getRelationships(id,
              initialIndex, numberOfFriends);
        
       if (newFriends == null ){
           error = true;
           message = "There was a problem with the database when retrevieng your friends list";
           friends = new ArrayList<User>();
       }else {
           error = false; 
           friends=newFriends;
       }      
    }
    
    public List<User> loadFriends(int userId){
        if (this.userId == -1){
            this.userId = userId;
            getFriendsFromDB(userId);
            hugs = Hug.getUserHugs(userId, 0, 10);
        }
        
        return friends;
    }
    
    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }

    public String getSearch() {
        return search;
    }

    public void setSearch(String search) {
        this.search = search;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }  

    public boolean isDisplayMessage() {
        return displayMessage;
    }

    public void setDisplayMessage(boolean displayMessage) {
        this.displayMessage = displayMessage;
    }

    public List<User> getHugs() {
        return hugs;
    }

    public void setHugs(List<User> hugs) {
        this.hugs = hugs;
    }
    
    
    public void next(){        
        error = false;
        if(friends.size() == numberOfFriends){
            initialIndex+=numberOfFriends;
        }else {
            return;
        }                           
        getFriendsFromDB(userId);               
        displayMessage = false;
    }
    
    public void previous(){
        if (initialIndex <= numberOfFriends) 
            initialIndex = 0;
        else 
            initialIndex-=numberOfFriends;
        error = false;
        displayMessage = false;
        getFriendsFromDB(userId);                   
    }
    
    public void addUser(User user){
        Relationship r = new Relationship(userId, user.getId());
        int status = r.insert();
        if (status >= 1){
            error = false;
            displayMessage = true;
            message = "User added to friends!";
            getFriendsFromDB(this.userId);
        }else {
            error = true;
            displayMessage = false;
            message = "User could not be added! They probably are already in your friend list!";
        }        
    }
    
    public void searchUsers(){              
       List<User> newUsers = User.getUsersByName(search, 0, 20);          
       if (newUsers == null ){
           error = true;
           users = new ArrayList<User>();
       }else {
           error = false;
           users=newUsers;
       }       
       displayMessage = false;
    }
    
    public void hugUser(User user){
        error = false;
        
        if (userId == -1 ) return;
        Hug hug = new Hug(userId, user.getId());
        int res = hug.insert();
        if (res == 1) {
            displayMessage = true;
            message = "User has been hugged";
        }else {
            error = true;
            displayMessage = false;
            message = "This user has been hugged, he hasn't seen the hug yet!";
        }
    }
    
    public void deleteHug(User user){
        new Hug(user.getId(),userId).delete();
        hugs.remove(user);
    }
     
}   
